package com.sptpc.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.sptpc.pojo.User;
import com.sptpc.service.UserService;

@Controller("userController")
public class UserController {
	
	@Autowired
	UserService userService;
	
	@RequestMapping("/loginout.action")
	public String loginOut(HttpServletRequest request,HttpServletResponse response) {
		request.getSession().invalidate();
		return "redirect:index.do";
	}
	
	@RequestMapping("/login.do")
	public String login(String loginName,String password,HttpServletRequest request,HttpServletResponse response) {
		System.out.println("loginName="+loginName);
		System.out.println("password="+password);
		User user = userService.userLogin(loginName,password);
		if(user != null) {
			request.getSession().setAttribute("session_user", user);
			return "redirect:index.do";
		}
		return "login";
	}
	
	@RequestMapping("/register.do")
	public String register(User user) {
		return "register";
	}
	
	@RequestMapping("/register.action")
	public String registerAction(String loginName,String passWord,String name,String sex,String address,String phone) {
//		User user = new User();
//		user.setUname(loginName);
//		user.setUpassword(password);
//		user.setRealname(name);
//		user.setSex(sex);
//		user.setAddress(address);
//		user.setPhone(phone);
		
		int a = userService.userRegister(loginName,passWord,name,sex,phone,address);
		
		return "login";
	}
}
